OpenRoads Designer CONNECT Edition SDK Help

Annotate in sheet model

The below code snippet shows annotating the element in sheet model. The code collects all elements from active sheet model and annotate the first complex element. The annotation position is calculated from sub element of complex element.

//Required References
using System;
using Bentley.DgnPlatformNET;
using Bentley.MstnPlatformNET;
using Bentley.GeometryNET;
using System.Diagnostics;
using Bentley.Interop.MicroStationDGN;

public void AnnotateCellsInSheet()
        {
            try
            {
                //Get active dgn model 
                DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
                if (dgnModel.ModelType == DgnModelType.Sheet)
                {
                    //Get all graphical elements cache from active model reference
                    Bentley.Interop.MicroStationDGN.ElementCache elementCache = Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.ActiveModelReference.GraphicalElementCache;

                    //Scan to get all elements  
                    ElementEnumerator elementEnumerator = elementCache.Scan();

                    //Add text annotation for first complex element
                    while (elementEnumerator.MoveNext())
                    {
                        //Get current element and check if it is of type cell
                        Bentley.Interop.MicroStationDGN.Element currentElement = elementEnumerator.Current;

                        //Get any complex element from sheet model to annotate
                        if (currentElement.IsComplexElement())
                        {
                            Bentley.Interop.MicroStationDGN.ComplexElement complexElement = currentElement.AsComplexElement();

                            if (complexElement != null)
                            {
                                ElementEnumerator elementIterator = complexElement.GetSubElements();
                                while (elementIterator.MoveNext())
                                {
                                    //Get sub elements to get position of element needed to set the origin for annotation 
                                    Bentley.Interop.MicroStationDGN.Element subElement = elementIterator.Current;

                                    if (subElement.IsLineElement())
                                    {
                                        Bentley.Interop.MicroStationDGN.LineElement lineElement = subElement.AsLineElement();
                                        double len = lineElement.Length / 2;
                                        Bentley.Interop.MicroStationDGN.Point3d point = lineElement.PointAtDistance(len);

                                        //User might need unit conversion here based on unit settings 
                                        DPoint3d origin = new DPoint3d(point.X, point.Y, point.Z);

                                        //Create new DgnTextStyle element for annotation definition
                                        DgnFile activeDgnFile = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnFile();
                                        DgnTextStyle textStyle = DgnTextStyle.GetByName("Sample Annotation", activeDgnFile);

                                        if (null == textStyle)
                                        {
                                            textStyle = new DgnTextStyle("Sample Annotation", activeDgnFile);
                                            textStyle.SetProperty(TextStyleProperty.Width, 400D);
                                            textStyle.SetProperty(TextStyleProperty.Height, 400D);
                                            textStyle.Add(activeDgnFile);
                                        }

                                        string textString = string.Format("Sample Annotation");

                                        //New TextBlock for defining annotation
                                        TextBlock textBlock = new TextBlock(textStyle, Session.Instance.GetActiveDgnModel());
                                        textBlock.AppendText(textString);
                                        textBlock.SetUserOrigin(origin);

                                        //Add text annotation
                                        Bentley.DgnPlatformNET.Elements.TextElement textElement = (Bentley.DgnPlatformNET.Elements.TextElement)Bentley.DgnPlatformNET.Elements.TextElement.CreateElement(null, textBlock);
                                        textElement.AddToModel();

                                        break;
                                    }

                                }

                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

Output